home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15046 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  108 lines

  1. Path: grimsel.zurich.ibm.com!usenet
  2. From: wgk@zurich.ibm.com (Keith Whittingham)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: doublelinked lists
  5. Date: 3 Apr 1996 09:35:28 GMT
  6. Organization: IBM Research, ZRH
  7. Message-ID: <4jtgp0$34f@grimsel.zurich.ibm.com>
  8. References: <4jrs9g$1pl@news-e2c.gnn.com>
  9. Reply-To: wgk@zurich.ibm.com
  10. NNTP-Posting-Host: pine.zurich.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.00
  12.  
  13. In <4jrs9g$1pl@news-e2c.gnn.com>, PLayton@gnn.com (Pam Layton) writes:
  14. >I need some help.  I have written a program for my C++ class.  It 
  15. >contains a struct for name information, a stuct for address 
  16. >information.  Then 3 classes, 1 for the name info, 1 for address 
  17. >info, and 1 for combining the two.  This creates a nice little 
  18. >address list.  I have the information stored in an array.  Now, I 
  19. >need to modify this program to use a double linked list instead of 
  20. >an array and be able to insert the information into the proper 
  21. >spot.  Alphabetically.  Does anyone know the best way I can go 
  22.  
  23. I don't know what compiler you're using but have a hunt around for
  24. collection classes - all the hard work's been done for you. If you
  25. don't have these or don't want to use them then no problem, writing
  26. your owns not that big a deal.
  27.  
  28. No you don't have to throw away your work - that's what C++ is all
  29. about. Write a linked list class and test it then derive your 
  30. classes from the "element" class in your linked list...
  31.  
  32.    class Element     // The elements of the linked list
  33.      {
  34.      public:
  35.        virtual void Print(stream &s) const = 0;
  36.        virtual ~Element() {};
  37.  
  38.      private:
  39.        Element *Nic;  // Next in chain (when this is member of list)
  40.      };
  41.  
  42.    class SortedList: // The sorted linked list
  43.      public Element  // Derived from Element, not really needed but nice...
  44.      {
  45.      public:
  46.        SortedList() { Fic = 0; }
  47.        virtual ~SortedList();
  48.        void Insert(Elemet &e);
  49.        virtual void Print(stream &s) const;
  50.  
  51.      private:
  52.        Element *Fic;  // First in chain
  53.      };
  54.  
  55. I've included a method (pure virtual) called print in the element which
  56. makes Element an Abstract Base Class (forcing Sorted list to define this
  57. so you can print the list).
  58.  
  59. Deriving SortedList from Element allows you to have a list of lists!
  60.  
  61. Note the list is called SortedList rather than DoubleLinkedList so that
  62. the implmentation is hidden - it is of little interest to the user of
  63. the class. Would single linked list be easier to implement (and so
  64. less bugs).
  65.    
  66. I've included an Insert method to insert a member in the list, you 
  67. probably also need Find() and Delete() methods. You also need to 
  68. worry about ownership of the elements: when I destruct a list do 
  69. I destruct the members?
  70.  
  71. The virtual destructers are a good idea on any function that has 
  72. virtual methods
  73.  
  74. Once tested and working derive your classes from Element:
  75.  
  76.    class Person:
  77.      public Element
  78.      {
  79.      public:
  80.         Name name;
  81.         Address address;
  82.      };
  83.  
  84.  
  85.    class Name:
  86.      public Element
  87.      {
  88.      ...
  89.  
  90. And Bob's your uncle - you might even have his address! You're code then
  91. looks like this...
  92.  
  93.    main(int argc, char *argv[])
  94.      {
  95.      SortedList AddressBook;
  96.      Person *Me = new Person("Keith", "Getysburg");
  97.      AddressBook.Insert(Me);
  98.      ...
  99.      }
  100.  
  101. I feel like a new Person.
  102.  
  103. Keith
  104.  
  105.  
  106.  
  107.  
  108.